package cn.edu.scau.cmi.chenzehan.client;
import cn.edu.scau.cmi.chenzehan.hibernateframe.DAO.StudentDAO;
import cn.edu.scau.cmi.chenzehan.hibernateframe.DAO.TutorDAO;
import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.Student;
import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.Tutor;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class UIHibernateFrame extends Application {

	private Button newButton;
	private Button newButton1;
	private Button newButton2;
	private Button updateButton;
	private Button updateButton1;
	private Button updateButton2;
	private Button searchButton;
	private Button searchButton1;
	private Button searchButton2;
	private Pane newPane;
	private Pane searchPane;
	private Pane updatePane;
	private TextField newStudentID;
	private TextField newStudentName;
	private TextField selectTeacher1;
	private TextField newTeacherID;
	private TextField newTeacherName;
	private TextField updateStudentID;
	private TextField updateStudentName;
	private TextField selectTeacher2;
	private TextField updateTeacherID;
	private TextField updateTeacherName;
	private TextField searchStudentID;
	private Button delect1;
	private TextField searchTeacherID;
	private Button delect2;
	
	private ListView lv;
	private boolean semaphore1 = false;
	private boolean semaphore2 = false;
	private ObservableList<String> dataList;
	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("hibernate.fxml"));
		
		lv = (ListView) root.lookup("#lv");
		dataList = FXCollections.observableArrayList();
		newButton = (Button)root.lookup("#newButton");
		newButton1 = (Button)root.lookup("#newButton1");
		newButton2 = (Button)root.lookup("#newButton2");
		updateButton = (Button)root.lookup("#updateButton");
		updateButton1 = (Button)root.lookup("#updateButton1");
		updateButton2 = (Button)root.lookup("#updateButton2");
		searchButton = (Button)root.lookup("#searchButton");
		searchButton1 = (Button)root.lookup("#searchButton1");
		searchButton2 = (Button)root.lookup("#searchButton2");
		newPane = (Pane) root.lookup("#newPane");
		searchPane = (Pane) root.lookup("#searchPane");
		updatePane = (Pane) root.lookup("#updatePane");
		newStudentID = (TextField)root.lookup("#newStudentID");
		newStudentName = (TextField)root.lookup("#newStudentName");
		selectTeacher1 = (TextField)root.lookup("#selectTeacher1");
		newTeacherID = (TextField)root.lookup("#newTeacherID");
		newTeacherName = (TextField)root.lookup("#newTeacherName");
		updateStudentID = (TextField)root.lookup("#updateStudentID");
		updateStudentName = (TextField)root.lookup("#updateStudentName");
		selectTeacher2 = (TextField)root.lookup("#selectTeacher2");
		updateTeacherID = (TextField)root.lookup("#updateTeacherID");
		updateTeacherName = (TextField)root.lookup("#updateTeacherName");
		searchStudentID = (TextField)root.lookup("#searchStudentID");
		searchTeacherID = (TextField)root.lookup("#searchTeacherID");
		delect1 = (Button)root.lookup("#delect1");
		delect2 = (Button)root.lookup("#delect2");
		
		StudentDAO s = new StudentDAO();
		TutorDAO t = new TutorDAO();
		newPane.setVisible(false);
		searchPane.setVisible(false);
		updatePane.setVisible(false);
		lv.setItems(dataList);
		
		
		newButton.setOnAction(e->{
			newPane.setVisible(true);
			searchPane.setVisible(false);
			updatePane.setVisible(false);
		});
		searchButton.setOnAction(e->{
			newPane.setVisible(false);
			searchPane.setVisible(true);
			updatePane.setVisible(false);
		});
		updateButton.setOnAction(e->{
			newPane.setVisible(false);
			searchPane.setVisible(false);
			updatePane.setVisible(true);
		});
		newButton1.setOnAction(e->{
			dataList.add("开始新建学生信息");
			if(newStudentID.getText().equals("")) {
				dataList.add("学号不能为空");
			}
			boolean flag = true;
			Tutor tutor = new Tutor();
			Student student = new Student();
			student.setName(newStudentName.getText());
			student.setStudentID(Integer.parseInt(newStudentID.getText()));
			if(!selectTeacher1.getText().equals("")) {
				tutor=t.findById(Integer.parseInt(selectTeacher1.getText()));
				if(tutor == null) {
					flag = false;
				}
				student.setTutor(tutor);
			}
			if(flag) {
				s.save(student);
			    dataList.add("成功保存Student： ID: "+student.getStudentID()+"  name:  "+student.getName());
			}
			else {
				dataList.add("保存失败,该工号教师不存在");
				flag = true;
			}
		});
		newButton2.setOnAction(e->{
			dataList.add("开始新建教师信息");
			
			Tutor tutor = new Tutor();
			if(newTeacherID.getText().equals("")) {
				dataList.add("教师工号不能为空");
			}
			else {
			        tutor = t.findById(Integer.parseInt(newTeacherID.getText()));
				if (tutor != null) {
					dataList.add("该教师ID已被注册");
					 }
				else {
					tutor = new Tutor(Integer.parseInt(newTeacherID.getText()),newTeacherName.getText());
       				t.save(tutor);
					dataList.add("成功保存Tutor： ID: "+tutor.getTutorID()+"  name:  "+tutor.getName());
					}
				}
		});
		updateButton1.setOnAction(e->{
			dataList.add("开始修改学生信息");
			if(updateStudentID.getText().equals("")) {
				dataList.add("学生学号不能为空");
			}
			else {
				if (s.findById(Integer.parseInt(updateStudentID.getText())) == null) {
					dataList.add("该学号的学生不存在");
				}
				else {
					if(t.findById(Integer.parseInt(selectTeacher2.getText())) == null) {
						dataList.add("该工号老师不存在");
					}
					else {
						Tutor tutor = t.findById(Integer.parseInt(selectTeacher2.getText()));
						Student student = new Student(Integer.parseInt(updateStudentID.getText()),updateStudentName.getText(),tutor);
						s.update(student);
						dataList.add("成功修改Student： ID: "+student.getStudentID()+"  name:  "+student.getName());
					}
				}
			}
		});
		updateButton2.setOnAction(e->{
			dataList.add("开始修改教师信息");
			if(updateTeacherID.getText().equals("")) {
				dataList.add("教师工号不能为空");
			}
			else {
				if(t.findById(Integer.parseInt(updateTeacherID.getText())) == null) {
					dataList.add("该工号老师不存在");
				}
				else {
					Tutor tutor = new Tutor(Integer.parseInt(updateTeacherID.getText()),updateTeacherName.getText());
					t.update(tutor);
					dataList.add("成功修改Tutor： ID: "+tutor.getTutorID()+"  name: "+tutor.getName());
				}
			}
		});
		searchButton1.setOnAction(e->{
			dataList.add("开始查询/删除 学生信息");
			if(searchStudentID.getText().equals("")) {
				dataList.add("待查询学生学号不能为空");
			}
			else {
				if(s.findById(Integer.parseInt(searchStudentID.getText())) == null) {
					dataList.add("该学号学生不存在");
				}
				else {
					Student student = s.findById(Integer.parseInt(searchStudentID.getText()));
					dataList.add("成功查询Student： ID: "+student.getStudentID()+"  name:  "+student.getName());
					semaphore1 = true;
				}
			}
		});
		delect1.setOnAction(e->{
			if (semaphore1) {
				Student student = s.findById(Integer.parseInt(searchStudentID.getText()));
				s.delete(student);
				dataList.add("成功删除");
				semaphore1 = false;
			}
			else {
				dataList.add("无效操作");
			}
		});
		searchButton2.setOnAction(e->{
			dataList.add("开始查询/删除 教师信息");
			if(searchTeacherID.getText().equals("")) {
				dataList.add("待查询教师工号不能为空");
			}
			else {
				if(t.findById(Integer.parseInt(searchTeacherID.getText())) == null) {
					dataList.add("该工号教师不存在");
				}
				else {
					Tutor tutor = t.findById(Integer.parseInt(searchTeacherID.getText()));
					dataList.add("成功查询Tutor： ID: "+tutor.getTutorID()+"  name:  "+tutor.getName());
					semaphore2 = true;
				}
			}
		});
		delect2.setOnAction(e->{
			if (semaphore2) {
				Tutor tutor = t.findById(Integer.parseInt(searchTeacherID.getText()));
				t.delete(tutor);
				dataList.add("成功删除");
				semaphore2 = false;
			}
			else {
				dataList.add("无效操作");
			}
		});
		
		Scene scene = new Scene(root);
		primaryStage.setTitle("Hibernate实验");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
